package net.onest.ch02sqlitedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText edtWord, edtTrans;
    private Button btnInsert, btnQuery;
    private TextView tvResult;
    private MyListener listener;
    //声明数据库对象
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件对象
        findViews();
        //注册监听器
        setListeners();
        //初始化数据库
//        initDB();
        //使用SQLiteOpenHelper管理数据库时初始化数据库的方法
        initDBByDBHelper();
    }

    private void initDBByDBHelper() {
        //初始化自定义的SQLiteOpenHelper对象
        MyDBHelper helper = new MyDBHelper(
                this,
                "WordDB.db",
                null,
                1
        );
        //获取数据库对象
        db = helper.getWritableDatabase();
    }

    private void initDB() {
        //设置数据库存储路径
        String dbFile = getFilesDir().getAbsolutePath() + "/WordDB.db";
        //获取数据库对象
        db = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
        //创建数据表
        //如果表存在就删除
        String dropIfExist = "drop table if exists words";
        db.execSQL(dropIfExist);
        String table = "create table words(" +
                "id integer primary key autoincrement," +
                "word varchar(30) not null," +
                "translation varchar(100) not null)";
        db.execSQL(table);
        Toast.makeText(this,
                "数据库初始化成功",
                Toast.LENGTH_SHORT).show();
    }

    private void setListeners() {
        listener = new MyListener();
        btnQuery.setOnClickListener(listener);
        btnInsert.setOnClickListener(listener);
    }

    private void findViews() {
        edtWord = findViewById(R.id.edt_word);
        edtTrans = findViewById(R.id.edt_trans);
        btnInsert = findViewById(R.id.btn_insert);
        btnQuery = findViewById(R.id.btn_query);
        tvResult = findViewById(R.id.tv_result);
    }
    class MyListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn_insert:
                    //录入新单词
                    insertWord();
                    break;
                case R.id.btn_query:
                    //查询所有单词
                    queryAllWord();
                    break;
            }
        }
    }

    private void queryAllWord() {
        //执行查询
        Cursor cursor = db.query("words", null, null, null, null, null, null);
        if(cursor.getCount() == 0){
            tvResult.setText("no word");
        } else {
            //遍历结果集
            StringBuffer buffer = new StringBuffer();
            while (cursor.moveToNext()) {
                //获取当前记录的单词和含义
                String word = cursor.getString(1);
                //根据字段名称获取字段的id
                int id = cursor.getColumnIndex("translation");
                String trans = cursor.getString(id);
                buffer.append(word + ": " + trans + "\n");
            }
            //显示结果
            tvResult.setText(buffer.toString());
        }
    }

    private void insertWord() {
        //获取单词和含义
        String word = edtWord.getText().toString();
        String trans = edtTrans.getText().toString();
        //将单词数据封装到ContentValues对象中
        ContentValues cv = new ContentValues();
        cv.put("word", word);
        cv.put("translation", trans);
        //执行插入操作
        long id = db.insert("words", null, cv);
        if(id > 0){
            //插入成功
            edtWord.setText("");
            edtTrans.setText("");
        }
    }
}